<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [//
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param \Exception $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        if ($this->shouldReport($exception)){
            $this->sendEmail($exception); // 发送邮件通知
        }
        parent::report($exception);
    }

    public function sendEmail(Throwable $exception)
    {
        try{
            $content = $exception->getMessage();
            $mail = '1522607807@qq.com';
            $title = '故事国系统错误';
            Log::info('sss');

            Mail::raw($content, function ($message) use ($mail, $title){
                $message->to($mail)->subject($title);
            });
        }catch(Exception $e){
            // 避免邮件发送失败导致无限循环
            \Log::error('发送异常邮件失败: ' . $e->getMessage());
        }
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Exception $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
}
